// 可能是我的node版本问题，不用严格模式使用ES6语法会报错
"use strict";
const models = require('../data/db')
const express = require('express')
const path = require('path')
const FileUtils = require('../util/FileUtils')
const builder = require('../response/rest.result.builder')
const utils = require('../util/utils')
const fs = require('fs')
const fsExtra = require('fs-extra')
/************** 创建(create) 读取(get) 更新(update) 删除(delete) **************/


module.exports = {
  getFileTree: function (req, res, next) {
    var filePath = req.query.path;
    var isAll = req.query.all === "1";
    if (!filePath) {
      filePath = '.'
    }
    var data = [{
      label: path.basename(FileUtils.getPath(filePath)),
      state: 'open',
      href: filePath,
      expand: true,
      children: utils.getFileTree(filePath, isAll)
    }]
    return builder.buildSuccessResultInfoWithData(res,data);
  },
  getFileContent: function (req, res, next) {
    // res.setHeader('content-type', 'text/html');
    var path = req.query.path
    var data = utils.getFileContent(path)
    return builder.buildSuccessResultInfoWithData(res,data);
  },
  addFile: function (req, res, next) {
    var newFileName = FileUtils.getPath(req.body.path, req.body.fileName)
    var isDir = req.body.isDir == '1'
    var method = isDir ? 'ensureDir' : 'ensureFile'
    console.log(req.body);
    fsExtra[method].call(fsExtra, newFileName, function(err) {
      if (err) {
        builder.buildFailedResultInfo(res)
      } else {
        var encoding = req.body.encoding || FileUtils.getFileEncoding(newFileName);
        FileUtils.writeFileContent(newFileName, req.body.content, encoding);
        builder.buildSuccessResultInfoWithData(res, {href: newFileName})
      }
    })
  },
  deleteFile: function (req, res, next) {
    var obj = req.query,
      currentName = FileUtils.getPath(obj.href)
    if (!fs.existsSync(currentName)) {
      builder.buildFailedResultInfo(res, 404, '无法找到对应文件或目录');
    } else {
      try {
        FileUtils.removeFile(currentName)
        builder.buildSuccessResultInfo(res);
      } catch (e) {
        builder.buildFailedResultInfo(res, 400, '删除文件失败');
      }
    }
  },
  saveFile: function (req, res, next) {
    var obj = { code: -1, message: '保存失败，请稍后再试～～' }
    console.log(req.body)
    if (req.body && req.body.href) {
      var currentName = FileUtils.getPath(req.body.href)
      var encoding = req.body.encoding || FileUtils.getFileEncoding(currentName)
      var currentTime = FileUtils.getLastModifiedTime(currentName)
      var oldTime = req.body.mtime
      /*console.log(oldTime);
      console.log(currentTime)
      if (oldTime != currentTime) {
        obj.code == -1
        obj.message = '保存失败！当前文档已过期，请刷新页面～～'
      } else {*/
        FileUtils.writeFileContent(currentName, req.body.content, encoding);
        obj.code = 1
        obj.message = 'success'
        obj.mtime = FileUtils.getLastModifiedTime(currentName)
      /*}*/
      builder.buildSuccessResultInfoWithData(res, obj)
    } else {
      builder.buildSuccessResultInfoWithData(res, obj)
    }
  }
};
